<template>
    <div class="order-report">
        <div class="report-table-top-title">终端设备管理</div>
        <div class="report-table-form1">
            <el-form
                ref="form"
                size="small"
                inline
                :model="form"
                label-width="80px"
            >
                <el-form-item label="SN码:" prop="chipID">
                    <el-input
                        type="text"
                        v-model="form.chipID"
                        class="item"
                    ></el-input>
                </el-form-item>
                <el-form-item label="商家编码:" prop="companyCode">
                    <el-input
                        type="text"
                        v-model="form.companyCode"
                        class="item"
                    ></el-input>
                </el-form-item>
                <el-form-item label="商家名称:" prop="companyName">
                    <el-input
                        type="text"
                        v-model="form.companyName"
                        class="item"
                    ></el-input>
                </el-form-item>

                <el-form-item label="经营类型:" prop="managementType">
                    <el-select
                        v-model="form.managementType"
                        class="item"
                        placeholder="请选择"
                    >
                        <el-option label="全部" value></el-option>
                        <el-option
                            v-for="(item, index) in $store.state.typeList"
                            :key="index"
                            :label="item.managementTypeName"
                            :value="item.managementType"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="状态:" prop="status">
                    <el-select
                        v-model="form.status"
                        class="item"
                        placeholder="请选择"
                    >
                        <el-option label="全部" value></el-option>
                        <el-option
                            v-for="(item, index) in statusList"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="设备类型:" prop="terminalType">
                    <el-select
                        v-model="form.terminalType"
                        class="item"
                        style="margin-right: 5px"
                        placeholder="请选择"
                    >
                        <el-option label="全部" value></el-option>
                        <el-option
                            v-for="(item, index) in terminalTypeList"
                            :key="index"
                            :label="item.terminalTypeName"
                            :value="item.terminalType"
                        ></el-option>
                    </el-select>

                    <el-button
                        type="primary"
                        size="small"
                        @click.stop="
                            position = 1
                            getList()
                        "
                        >查询</el-button
                    >
                    <el-button
                        type="primary"
                        size="small"
                        @click.stop="resetFields"
                        >重置</el-button
                    >
                </el-form-item>
            </el-form>
        </div>
        <div class="page-padding-gray-small"></div>
        <div class="report-table-wrapper">
            <el-table
                :header-cell-style="headerCellStyle"
                size="small"
                :data="showList"
                border
                style="width: 100%"
            >
                <el-table-column
                    prop="companyCode"
                    label="商家编码"
                ></el-table-column>
                <el-table-column
                    prop="companyName"
                    label="商家名称"
                ></el-table-column>
                <el-table-column
                    prop="agentName"
                    label="代理商"
                ></el-table-column>
                <el-table-column prop="chipID" label="SN码"></el-table-column>
                <el-table-column
                    prop="managementTypeName"
                    label="经营类型"
                ></el-table-column>
                <el-table-column
                    prop="terminalTypeName"
                    label="设备类型"
                ></el-table-column>
                <el-table-column
                    prop="statusName"
                    label="状态"
                ></el-table-column>
                <el-table-column label="操作" fixed="right" width="100px">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            v-if="scope.row.status === 4"
                            type="primary"
                            @click="upStatus(2, scope.row.chipID)"
                            >启用</el-button
                        >
                        <el-button
                            size="mini"
                            v-if="scope.row.status === 2"
                            type="primary"
                            @click="upStatus(4, scope.row.chipID)"
                            >停用</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="report-table-block">
            <div class="report-table-block-in">
                <el-pagination
                    background
                    :current-page.sync="position"
                    @current-change="handleCurrentChange"
                    :page-size="10"
                    layout="prev, pager, next, total"
                    :total="totalNum"
                ></el-pagination>
            </div>
        </div>
        <div class="page-padding"></div>
    </div>
</template>

<script type="text/ecmascript-6">
export default {
    data: function () {
        return {
            currRow: {},
            showValue: false,
            totalNum: 0,
            position: 1,
            form: {
                chipID: '',
                companyCode: '',
                companyName: '',
                managementType: '',
                status: '',
                terminalType: ''
            },
            statusList: [],
            terminalTypeList: [],
            showList: []
        }
    },
    mounted () {
    },
    created () {
        this.getOnlyTerminalType()
        this.terminalStatusList()
        this.getList()
    },
    watch: {
        position () {
            this.getList()
        }
    },
    components: {
    },
    methods: {
        getFileType: function (filePath) {
            var startIndex = filePath.lastIndexOf('.')
            if (startIndex !== -1) {
                return filePath.substring(startIndex + 1, filePath.length).toLowerCase()
            } else {
                return ''
            }
        },
        beforeUpload (file) {
            let fileType = this.getFileType(file.name)
            console.log(fileType)
            if (fileType !== 'xls' && fileType !== 'xlsx') {
                this.$message.warning('请上传.xlsx或者.xls格式文件')
                return false
            }
            return true
        },
        async fnUploadRequest (option) {
            console.log(option)
            let file = option.file
            if (!this.beforeUpload(file)) {
                this.$refs.upload.uploadFiles = []
                return false
            }
            console.log('65666')
            let formData = new FormData()
            formData.append('file', file)
            let res = await this.$http.post(this.$store.state.IP + '/terminalSn/importSn', formData)
            let response = res.data
            if (response.ret === '0') {
                console.log(response)
                this.getList()
            }
        },
        upStatus (status, chipID) {
            this.loading = true
            this.$confirm('确定更改状态吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.loading = true
                this.$http.post(this.$store.state.IP + '/terminalDeviceManage/updateTerminalStatus', {
                    status: status,
                    chipID: chipID
                },
                    {
                        'emulateJSON': false
                    }
                ).then(response => {
                    response = response.body
                    if (response.ret === '0') {
                        this.$message({
                            showClose: true,
                            message: '操作成功',
                            type: 'success'
                        })
                        this.getList()
                        this.clearData()
                    }
                    if (response.ret !== '0') {
                        console.log('保存失败')
                    }
                }, response => {
                    console.log('调用保存失败')
                })
            }).catch(() => {
            })
        },
        resetFields () {
            this.$refs['form'].resetFields()
            this.position = 1
            this.getList()
        },
        getOnlyTerminalType () {
            this.$http.get(this.$store.state.IP + '/system/company/getOnlyTerminalType?isAll=1', {
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    this.terminalTypeList = response.data
                }
            }, response => {
            })
        },
        // 换页方法
        handleCurrentChange (val) {
            this.position = val
        },
        terminalStatusList () {
            this.$http.get(this.$store.state.IP + '/terminalDeviceManage/terminalStatusList', {
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    console.log('调用库存列表成功')
                    console.log(response.data)
                    this.statusList = response.res
                }
                if (response.ret !== '0') {
                    console.log('调用库存列表失败')
                }
                console.log('******************************************')
            }, response => {
                console.log('通讯失败')
            })
        },
        getList () {
            this.$http.post(this.$store.state.IP + '/terminalDeviceManage/findTerminalPage', {
                ...this.form,
                page: this.position - 1, // 页数
                size: 10 // 一页显示数量

            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    console.log('调用库存列表成功')
                    console.log(response.data)
                    this.showList = response.res.list
                    this.totalNum = response.res.total
                }
                if (response.ret !== '0') {
                    console.log('调用库存列表失败')
                }
                console.log('******************************************')
            }, response => {
                console.log('通讯失败')
            })
        }
    }
}
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.flex
    display: flex

.item
    width: 200px

.order-report
    width: 100%
    background: #fff
    min-height: calc(100vh - 65px)

    .report-table-top-title
        width: 100%
        height: 64px
        line-height: 64px
        color: #000
        font-size: 18px
        background: #f1f0f5

    .report-table-form1
        width: 100%
        min-height: 40px
        padding-top: 15px
        box-sizing: border-box
        background: #fff
        overflow: hidden

        .report-table-form1-line
            min-height: 32px
            margin-bottom: 16px

            .report-table-form1-item
                display: inline-block
                vertical-align: top
                height: 32px
                margin-right: 15px
                line-height: 32px

                &.word
                    margin-right: 0

                &.samll
                    font-size: 15px
                    text-align: left

                &.big
                    margin-right: 100px

    .report-table-block
        width: 100%
        padding-top: 22px
        height: 50px
        background: #fff
        box-sizing: border-box

        .report-table-block-in
            float: right
            padding-right: 17px

    .page-padding-gray-small
        width: 100%
        height: 22px
        background: #f1f0f5

    .page-padding
        width: 100%
        height: 20px
        background: #fff

    .report-table-total
        width: 100%
        font-size: 16px
        color: #409EFF
        padding: 22px
        margin-top: 22px
        box-sizing: border-box
        background: #fff
</style>
